package com.demo.filter;

import com.demo.dto.UserDetailDto;
import com.demo.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.UUID;


public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
    private AuthenticationManager authenticationManager;

    private JwtUtils jwtUtils;

    public JWTAuthenticationFilter(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
        //设置自定义登录地址，此地址非控制器中的地址，如果需要自定义使用控制器中的地址
        super.setFilterProcessesUrl("/user/subLogin");
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        try {
            System.out.println("=========正在验证登录信息=======================");
            Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(request.getParameter("username"), request.getParameter("password")));
            System.out.println("验证成功");
            System.out.println(authentication);
            return authentication;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
                                            FilterChain chain, Authentication authResult) throws IOException, ServletException {
        UserDetailDto userDetailDto = (UserDetailDto) authResult.getPrincipal();
        System.out.println("=================userDetailDtos================");
        System.out.println(userDetailDto);
        String uuid = UUID.randomUUID().toString();
        System.out.println("++++++++++++UUID++++++++++++++++++++");
        jwtUtils = new JwtUtils();
        String token = jwtUtils.createToken(uuid, userDetailDto.getUsername(), userDetailDto, 3600);
        System.out.println("===========登录成功==================");
        System.out.println(token);
        response.setHeader(JwtUtils.TOKEN_HEADER, JwtUtils.TOKEN_PREFIX + token);
        response.sendRedirect("/index");
    }
}
